﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SchoolProject2012.Functions;
using SchoolProject2012.Areas.Eleve.Models;

namespace SchoolProject2012.Areas.Eleve.Controllers
{
    public class EventController : Controller
    {
        private EleveSchoolProjectBddEntities db = new EleveSchoolProjectBddEntities();
        //
        // GET: /Eleve/Event/
        // Affiche tous les événements d'un élève
        [Authorize(Roles = "Admin, Eleve, Delegue, BDE")]
        public ActionResult Index(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string login = Session["login"].ToString();
                return View();
            }
            catch
            {
                return View("error");
            }
        }

        // GET: /Eleve/Event/coursPrev
        // Affiche les 3 derniers cours d'une classe
        [Authorize(Roles = "Admin, Eleve, Delegue, BDE")]
        public ViewResult coursPrev(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string login = Session["login"].ToString();
                //Changement kris à voir
                int idClasse = db.ELEVE.Single(x => x.LOGINPERS == login).ELEVECLASS.Single(c => c.CLASSE.ANNEECLASSE == Utils.getCurrentYearForClass()).IDCLASS;
                //Changement kris à voir
                var coursPrev = db.COURS.Where(x => x.DATE_DBT < DateTime.Now && x.MODULE.CLASSE.IDCLASSE == idClasse).Take(3).OrderBy(x => x.DATE_DBT).ToList();
                return View(coursPrev);
            }
            catch
            {
                return View("error");
            }
        }

        // GET: /Eleve/Event/coursNext
        // Affiche tous les 3 prochains cours d'une classe
        [Authorize(Roles = "Admin, Eleve, Delegue, BDE")]
        public ViewResult coursNext(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string login = Session["login"].ToString();
                //Changement kris à voir

                int idClasse = db.ELEVE.Single(x => x.LOGINPERS == login).ELEVECLASS.Single(c => c.CLASSE.ANNEECLASSE == Utils.getCurrentYearForClass()).IDCLASS;
                var coursNext = db.COURS.Where(x => x.DATE_DBT > DateTime.Now && x.MODULE.CLASSE.IDCLASSE == idClasse).Take(3).OrderBy(x => x.DATE_DBT).ToList();
                return View(coursNext);
            }
            catch
            {
                return View("error");
            }
        }

        // GET: /Eleve/Event/examsPrev
        // Affiche tous les 3 précédentes évaluations d'une classe
        [Authorize(Roles = "Admin, Eleve, Delegue, BDE")]
        public ViewResult examsPrev(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string year = DateTime.Now.Year.ToString();
                string login = Session["login"].ToString();
                int idClasse = db.ELEVE.Single(e => e.LOGINPERS == login).ELEVECLASS.Single(c => c.CLASSE.ANNEECLASSE == Utils.getCurrentYearForClass()).IDCLASS;
                var exams = db.EVALUATION.Where(x => x.DATE_EVAL < DateTime.Now && x.MODULE.IDCLASSE == idClasse).ToList().Take(3).OrderBy(x => x.DATE_EVAL);
                return View(exams);
            }
            catch
            {
                return View("error");
            }
        }

        // GET: /Eleve/Event/examsNext
        // Affiche tous les 3 prochaines évaluations d'une classe
        [Authorize(Roles = "Admin, Eleve, Delegue, BDE")]
        public ViewResult examsNext(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string year = DateTime.Now.Year.ToString();
                string login = Session["login"].ToString();
                int idClasse = db.ELEVE.Single(e => e.LOGINPERS == login).ELEVECLASS.Single(c => c.CLASSE.ANNEECLASSE == Utils.getCurrentYearForClass()).IDCLASS;
                var exams = db.EVALUATION.Where(x => x.DATE_EVAL > DateTime.Now && x.MODULE.IDCLASSE == idClasse).ToList().Take(3).OrderBy(x => x.DATE_EVAL);
                return View(exams);
            }
            catch
            {
                return View("error");
            }
        }

        // GET: /Eleve/Event/eventNext
        // Affiche tous les 3 prochains événements d'une classe
        [Authorize(Roles = "Admin, Eleve, Delegue, BDE")]
        public ViewResult eventNext(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string login = Session["login"].ToString();
                int idClasse = db.ELEVE.Single(e => e.LOGINPERS == login).ELEVECLASS.Single(c => c.CLASSE.ANNEECLASSE == Utils.getCurrentYearForClass()).IDCLASS;
                var eventNext = db.EVENEMENT.Where(x => x.DATE_DBT_EVE >= DateTime.Now).Take(3).ToList().OrderBy(x => x.DATE_DBT_EVE);
                return View(eventNext);
            }
            catch
            {
                return View("error");
            }
        }

        // GET: /Eleve/Event/eventPrev
        // Affiche tous les 3 précédents événement d'une classe
        [Authorize(Roles = "Admin, Eleve, Delegue, BDE")]
        public ViewResult eventPrev(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string login = Session["login"].ToString();
                int idClasse = db.ELEVE.Single(e => e.LOGINPERS == login).ELEVECLASS.Single(c => c.CLASSE.ANNEECLASSE == Utils.getCurrentYearForClass()).IDCLASS;
                var eventNext = db.EVENEMENT.Where(x => x.DATE_FIN_EVE <= DateTime.Now).Take(3).ToList().OrderByDescending(x => x.DATE_FIN_EVE);
                return View(eventNext);
            }

            catch
            {
                return View("error");
            }
        }

        // GET: /Eleve/Event/projetNext
        // Affiche tous les 2 prochains projets d'une classe
        [Authorize(Roles = "Admin, Eleve, Delegue, BDE")]
        public ViewResult projetNext(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string login = Session["login"].ToString();
                int idClasse = db.ELEVE.Single(e => e.LOGINPERS == login).ELEVECLASS.Single(c => c.CLASSE.ANNEECLASSE == Utils.getCurrentYearForClass()).IDCLASS;
                var projet = db.PROJET.Where(x => x.MODULE.CLASSE.IDCLASSE == idClasse && x.DATE_DBT_PROJ >= DateTime.Now).Take(2).ToList().OrderBy(x => x.DATE_DBT_PROJ);
                return View(projet);
            }
            catch
            {
                return View("error");
            }
        }

        // GET: /Eleve/Event/addEvent
        // Formulaire pour créer un événement
        [Authorize(Roles = "Admin, Delegue, BDE")]
        public ViewResult addEvent(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                return View();
            }
            catch
            {
                return View("error");
            }
        }

        // GET: /Eleve/Event/addEvent
        // Validation du formulaire
        [Authorize(Roles = "Admin, Delegue, BDE")]
        [HttpPost]
        public ViewResult addEvent(string langue, EVENEMENT eve)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                if (ModelState.IsValid)
                {
                    string login = Session["login"].ToString();
                    ELEVE e = db.ELEVE.Single(x => x.LOGINPERS == login);
                    //eve.LOGINPERS_CREATE = e.LOGINPERS;
                    db.AddToEVENEMENT(eve);
                    db.SaveChanges();
                    return View("detailsEvent", eve);
                }
                else
                {
                    return View();
                }
            }
            catch
            {
                return View("error");
            }
        }

        // GET: /Eleve/Event/detailsEvent
        // Affiche le détail d'un événement
        [Authorize(Roles = "Admin, Eleve, Delegue, BDE")]
        public ViewResult detailsEvent(string langue, int id)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                var eve = db.EVENEMENT.Single(x => x.ID_EVE == id);
                return View(eve);
            }
            catch
            {
                return View("error");
            }
        }
    }
}
